package collection.listtest;

import java.util.ArrayList;
import java.util.List;

public class SubListDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        list.add("two");
        System.out.println("list:"+list);

        //截取list的子集
        List<String> subList = list.subList(1,4);
        System.out.println("subList:"+subList);

        //原集合修改子集也跟着变
        list.set(1,"six");
        System.out.println("list修改后："+list);
        System.out.println("subList:"+subList);

        //子集修改后原集合也变
        subList.set(0,"6");
        System.out.println("list:"+list);
        System.out.println("subList修改后:"+subList);

        //子集增加或删除元素后 原集合会跟着变
        subList.remove(0);
        System.out.println("list："+list);

        //原集合增加或者删除元素后  原子集就不能再有任何操作了 否则运行时会报异常
        list.add("nine");
        System.out.println(subList);
    }
}
